
import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class PlayGame extends JFrame {
    public  List player1=new ArrayList<>();
    public  List player2=new ArrayList<>();
    public  List player3=new ArrayList<>();
    public  List base=new ArrayList<>();
    public List<Card> pukes = new ArrayList<>();

    public PlayGame() {
        init();
        xipai();
        fapai();
        this.setLayout(null);
        for (int i = 0; i < player1.size(); i++) {
            Icon btnImg = Util.getIcon(player1.get(i).toString());
            JButton imageBtn = new JButton(btnImg);
            imageBtn.setBounds(10, 500- ((i + 1) * 15), 105, 150);
            this.add(imageBtn);
        }
        for (int i = 0; i < player2.size(); i++) {
            Icon btnImg = Util.getIcon(player2.get(i).toString());
            JButton imageBtn1 = new JButton(btnImg);
            imageBtn1.setBounds(500- ((i + 1) * 15), 500, 105, 150);
            this.add(imageBtn1);
        }
        for (int i = 0; i < player3.size(); i++) {
            Icon btnImg = Util.getIcon(player3.get(i).toString());
            JButton imageBtn2 = new JButton(btnImg);
            imageBtn2.setBounds(675, 500- ((i + 1) * 15) , 105, 150);
            this.add(imageBtn2);
        }
        for (int i = 0; i < base.size(); i++) {
            Icon btnImg = Util.getIcon(base.get(i).toString());
            JButton imageBtn2 = new JButton(btnImg);
            imageBtn2.setBounds(400-((i + 1) * 15), 10 , 105, 150);
            this.add(imageBtn2);
        }
        this.setSize(800, 800);
        this.setTitle("PGmdfkone1");
        this.setLocationRelativeTo((Component) null);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    // 初始化54张扑克牌
    public void init() {
        String[] points = "3,4,5,6,7,8,9,10,J,Q,K,A,2".split(",");
        String[] types = "♠,♥,♣,♦".split(",");
        for (int i = 0; i < points.length; i++) {
            for (int j = 0; j < types.length; j++) {
                String path = "/img/" + (i + 3) + "_" + (j + 1) + ".jpg";
                pukes.add(new Card(points[i], types[j], i, j, path));
            }
        }
        pukes.add(new Card("大王", "", 14, 0, "/img/16_1.jpg"));
        pukes.add(new Card("小王", "", 13, 0, "/img/17_1.jpg"));
    }

    // 洗牌
    public void xipai() {
        Collections.shuffle(pukes);
    }

    // 发牌
    public void fapai() {
         player1 = pukes.subList(0, 17);
         player2 = pukes.subList(17, 34);
         player3 = pukes.subList(34, 51);
         base = pukes.subList(51, 54);

        sort(player1);
        sort(player2);
        sort(player3);
        sort(base);

    }

    public void sort(List list) {
        list.sort(new Comparator<Card>() {
            @Override
            public int compare(Card o1, Card o2) {
                if (o1.getPointValue() == o2.getPointValue()) {
                    return o1.getTypeValue() - o2.getTypeValue();
                } else {
                    return o2.getPointValue() - o1.getPointValue();
                }
            }
        });
    }

    public static void main(String[] args) {
 new PlayGame();
    }
}
